import java.util.Objects;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * user:likai
 * Date:2022-11-06
 * Time:16:47
 */
class Person{
    String id;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return Objects.equals(id, person.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }
}
public class HashBuckNew<K,V> {
    static class Node<K,V>{
        public K k;
        public V v;
        public Node next;

        public Node(K k, V v) {
            this.k = k;
            this.v = v;
        }
    }
    public Node<K,V>[] array=(Node<K,V>[])new Node[10];
    public int useSize;
    public void put(K k,V v){
        int index=k.hashCode()% array.length;
        //遍历看看里面有没有，有的话需要替换
        Node cur=array[index];
        while(cur!=null){
            if(cur.k.equals(k)){
                cur.v=v;
            }
            cur=cur.next;
        }
        //头插法
        Node node=new Node(k,v);
        node.next=array[index];
        array[index]=node;
        useSize++;
        //负载因子不能超过0.75
        if(loadFactor()>=0.75f){
            resize();
        }
    }
    private float loadFactor(){
        return useSize*1.0f/array.length;
    }
    private void resize(){
        Node[]newArray=new Node[2*array.length];
        for (int i = 0; i < array.length; i++) {
            Node cur=array[i];
            while(cur!=null){
                //Node curNext=cur.next;
                int newIndex=cur.hashCode()% newArray.length;
                Node newNode=new Node(cur.k,cur.v);
                newNode.next=newArray[newIndex];
                newArray[newIndex]=newNode;
                cur=cur.next;
            }
        }
        array=newArray;
    }
}
